﻿// -----------------------------------------------------------------------
// <copyright file="NativeMethods.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace DC2010_EditorInfo
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.Diagnostics;
    using System.Drawing;

    /// <summary>
    /// Encapsulation for API callings
    /// </summary>
    public static class NativeMethods
    {
        [DllImport("kernel32.dll")]
        internal static extern Boolean AllocConsole();

        [DllImport("user32.dll")]
        static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey,
           byte bAlpha, uint dwFlags);

        [DllImport("user32.dll")]
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

        [DllImport("user32.dll", SetLastError = true)]
        static extern System.UInt32 GetWindowLong(IntPtr hWnd, int nIndex);

        public const int GWL_EXSTYLE = -20;
        public const int WS_EX_LAYERED = 0x80000;
        public const int LWA_ALPHA = 0x2;
        public const int LWA_COLORKEY = 0x1;

        [StructLayout(LayoutKind.Sequential)]
        public struct DWM_BLURBEHIND
        {
            public DwmBlurBehindDwFlags dwFlags;
            public bool fEnable;
            public IntPtr hRgnBlur;
            public bool fTransitionOnMaximized;
        }

        [Flags()]
        public enum DwmBlurBehindDwFlags : uint
        {
            DWM_BB_ENABLE = 0x1,
            DWM_BB_BLURREGION = 0x2,
            DWM_BB_TRANSITIONONMAXIMIZED = 0x4
        }

        [DllImport("dwmapi.dll", PreserveSig = false)]
        public static extern void DwmEnableBlurBehindWindow(IntPtr hwnd, ref DWM_BLURBEHIND blurBehind);

        public static void EnableBlurBehind()
        {
            IntPtr Handle = Process.GetCurrentProcess().MainWindowHandle;
            DWM_BLURBEHIND blur = new DWM_BLURBEHIND();
            blur.dwFlags = DwmBlurBehindDwFlags.DWM_BB_ENABLE;// +DwmBlurBehindDwFlags.DWM_BB_TRANSITIONONMAXIMIZED;
            blur.fEnable = true;
            //blur.hRgnBlur = 0;
            blur.fTransitionOnMaximized = false;// DwmBlurBehindDwFlags.DWM_BB_TRANSITIONONMAXIMIZED;

            DwmEnableBlurBehindWindow(Handle, ref blur);
        }

        public static void MakeTransparent(byte pct)
        {
            IntPtr Handle = Process.GetCurrentProcess().MainWindowHandle;
            int newDwLong = ((int)GetWindowLong(Handle, GWL_EXSTYLE)) ^ WS_EX_LAYERED;
            SetWindowLong(Handle, GWL_EXSTYLE, newDwLong);
            SetLayeredWindowAttributes(Handle, 0, pct, LWA_ALPHA);
        }
    }
}
